package bank;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public enum KontoDB {
  
  SINGLETON;

  private final Map<String, Konto> alleKonten = new ConcurrentHashMap<String, Konto>();
  private final AtomicLong lastKontoId = new AtomicLong(1000000000L);


  private String generateNewKontoID() {

    return String.valueOf(lastKontoId.incrementAndGet());
  }

  String kontoEinrichten() throws RemoteException {

    String ktoNr = generateNewKontoID();
    String id = Bank.blz + "_" + ktoNr;

    Konto newKonto = new Konto(id);

    alleKonten.put(ktoNr, newKonto);
    try {
      Naming.rebind(id, newKonto);
    } catch (Exception e) {
      throw new RemoteException("Bankmanager::kontoEinrichten() " + "-> newKonto rebind failed\n" + e.getMessage());
    }
    return id;
  }

}
